package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.RechargeServiceItem;
import cn.wolfcode.crm.query.JSONResult;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.RechargeServiceItemQueryObject;
import cn.wolfcode.crm.service.IRechargeServiceItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("rechargeServiceItem")
public class RechargeServiceItemController {

    @Autowired
    private IRechargeServiceItemService rechargeServiceItemService;

    //由于dataGrid组件返回必须为json,所以跳转界面就分开
    @RequestMapping("view")
    public String view(){
        return "recharge";
    }


    //页面数据
    @RequestMapping("query")
    @ResponseBody
    public List<RechargeServiceItem> query(RechargeServiceItemQueryObject qo){
        return rechargeServiceItemService.query(qo);
    }


    //会员拥有的服务查询
    @RequestMapping("getService")
    @ResponseBody
    public List<Map<String,Object>> getService(Long clientId){
        return rechargeServiceItemService.getService(clientId);
    }

    //充次操作
    @RequestMapping("recharge")
    @ResponseBody
    public JSONResult recharge(RechargeServiceItem item){
        JSONResult result = new JSONResult();
        try{
            rechargeServiceItemService.recharge(item);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //获取充值之前的对应的服务及次数
    @RequestMapping("getBeforeCount")
    @ResponseBody
    public Integer getBeforeCount(Long clientId,String serviceName){
        Integer beforeCount = rechargeServiceItemService.getBeforeCount(clientId, serviceName);
        System.out.println(beforeCount);
        return beforeCount;
    }

}
